package com.zhengke.service.coolxinService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.Result;
import com.zhengke.common.object.ZTreeNode;
import com.zhengke.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * *******************************
 * 项目名称：tvmall-parent
 * 文件名称：JfdhServiceImpl
 * 类说明：  积分兑换
 * 创建时间：2020/1/11 15:42
 *
 * @author 王鑫
 * @version 1.0
 * *******************************
 */
@Service
public class JfdhServiceImpl implements JfdhService {
    @Autowired
    private CommonDao dao;

    @Override
    public List<ZTreeNode> queryParent(String parentId) throws Exception {
        List<SysArea> gcList = (List<SysArea>) dao.findForList("jfdhMapper.selectParentArea", Integer.parseInt(parentId));
        List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
        for (SysArea sa : gcList) {
            ZTreeNode item = new ZTreeNode(sa.getAreaName(), sa.getAreaId().toString());
            item.setIsParent(sa.getChildNum() > 0);
            rtnList.add(item);
        }
        System.out.println(rtnList);
        return rtnList;
    }

    @Override
    public Result queryMsg(String areaId, String goodsName, String property, int start, int size) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("servicePointId", Integer.parseInt(areaId));
        map.put("goodsName", goodsName);
        map.put("property", property);
        PageHelper.startPage(start, size, true);
        List<MallServiceGoods01> rtnList = (List<MallServiceGoods01>) dao.findForList("jfdhMapper02.selectByServicePointId", map);

        for (MallServiceGoods01 msg:rtnList) {
            String paramValue = msg.getParamValue().split(":")[0];//兑换比例
            BigDecimal price = msg.getGoodsPrice();
            msg.setGoodsExchIntegral(price.multiply(new BigDecimal(paramValue)));
            msg.setParamValue(paramValue);
        }
        PageInfo<MallServiceGoods01> pageInfo = new PageInfo<>(rtnList);
        Result rs = new Result();
        rs.setCode("0");
        rs.setCount(pageInfo.getTotal());
        rs.setData(rtnList);
        return rs;
    }

    /**
     * 查询TvUser
     *
     * @param cardNumber
     * @return
     */
    @Override
    public List<TvUser> queryUser(String cardNumber) throws Exception {
        List<TvUser> tvUserList = (List<TvUser>) dao.findForList("jfdhMapper03.selectByUcNumber", cardNumber);
        return tvUserList;
    }

    @Override
    public TvUser queryTvUser(String cardNumber) throws Exception {
        TvUser tvUser = (TvUser) dao.findForObject("jfdhMapper03.selectByUcNumber", cardNumber);
        return tvUser;
    }
    /**
     * 通过id查询会员
     * 返回对象
     */
    @Override
    public TvUser queryByUserId(String userId) throws Exception {
        return (TvUser) dao.findForObject("jfdhMapper03.queryByUserId", userId);
    }
    /**
     * 通过多个参数查询打包商品
     * @param goodsId
     * @param goodsType
     * @param servicePointId
     * @return
     */
    @Override
    public MallServiceGoods01 queryMsgByMap(int goodsId, int goodsType, int servicePointId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("goodsId",goodsId);
        map.put("goodsType",goodsType);
        map.put("servicePointId",servicePointId);
        return (MallServiceGoods01) dao.findForObject("jfdhMapper02.selectByMap",map);
    }

    /**
     * 生成订单
     *
     * @param to
     * @param tog
     * @param msg
     */
    @Override
    public void createOrder(TvOrder to, TvOrderGoods tog, MallServiceGoods01 msg, TvUser user) throws Exception {
        String orderSn = to.getOrderSn();
        //插入一条tvOrder数据
        int result = (int) dao.save("orderMapper.insertOneOrder", to);
        if (result > 0) {//表明插入成功
            int ordId = (int) dao.findForObject("orderMapper.selectOrderId", orderSn);
            tog.setOrderId(ordId);
            //插入一条TvOrderGoods数据
            dao.save("ddgl060Mapper.insertOneTog", tog);
            //更新一条MallServiceGoods库存和订单限额
            dao.update("jfdhMapper02.updateNumber", msg);
            //修改user积分，参数 userCardNumber
            dao.update("jfdhMapper03.updateByUCNumber", user);
        }

    }

    /**
     * 查询下单商品详情
     *
     * @param goodsId
     * @return
     */
    @Override
    public List<ToBuyMsg> queryToBuyMsg(String goodsId) throws Exception {
        List<ToBuyMsg> rtnList = (List<ToBuyMsg>) dao.findForList("jfdhMapper04.selectToBuyMsg", Integer.parseInt(goodsId));
        return rtnList;
    }

    @Override
    public ToBuyMsg queryToBuyMsgObj(String goodsId) throws Exception {
        ToBuyMsg toBuyMsgObj = (ToBuyMsg) dao.findForObject("jfdhMapper04.selectToBuyMsg", Integer.parseInt(goodsId));
        return toBuyMsgObj;
    }


}
